<template>
  <div class="m-header">
    <div class="logo">
      <Icon type="global" />
      XXXXXXX
    </div>
    <div>{{ user.userName }}</div>
    <div class="divider" />
    <Dropdown
      :trigger="['click']"
      overlayClassName="header-avatar"
      :getPopupContainer="(trigger) => trigger.parentNode"
      placement="bottomRight"
    >
      <div class="avatar">{{ nameLastWord }}</div>
      <template #overlay>
        <a-menu @click="dropMenuClick">
          <a-menu-item>
            <span
              :class="['avatarMini', 'inline-middle']"
              style="margin-right: 10px"
            >
              {{ nameLastWord }}
            </span>
            <span :class="['inline-middle', 'user-title']"
              >Hi, {{ user.userName }}</span
            >
          </a-menu-item>
          <Divider
            style="background-color: #e4e7ef; height: 0.5px; margin: 4px 0"
          />
          <a-menu-item key="logout">
            <Icon type="logout" style="margin-right: 10px" />
            <span :class="['inline-middle', 'user-item']">退出登录</span>
          </a-menu-item>
        </a-menu>
      </template>
    </Dropdown>
  </div>
</template>
<script>
import { Divider, Dropdown, Icon } from "ant-design-vue";

export default {
  name: "MHeader",
  components: { Dropdown, Divider, Icon },
  props: {},
  mounted() {},
  data() {
    return {
      user: {
        userName: "尊尼获加",
      },
    };
  },
  methods: {
    dropMenuClick(e) {
      if (e.key === "logout") {
        this.$router.push("/login");
      }
    },
  },
  computed: {
    nameLastWord() {
      return this.user.userName.charAt(this.user.userName.length - 1);
    },
  },
};
</script>
<style scoped lang="less">
.m-header {
  width: 100%;
  height: 50px;
  line-height: 50px;
  padding: 0 24px;
  box-shadow: 0 1px 1px 0 rgba(192, 196, 204, 0.2);
  background-color: #fff;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  z-index: 999;
  position: fixed;
  top: 0;
  left: 0;
  .logo {
    margin-right: auto;
    font-size: 20px;
    color: #0176f6;
  }
  .divider {
    width: 1px;
    height: 10px;
    background: #c5c9d5;
    margin: 0 16px;
  }

  .avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #0176f6;
    cursor: pointer;
    color: #fff;
    text-align: center;
    vertical-align: middle;
    line-height: 28px;
    font-size: 14px;
  }

  .avatarMini {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: #0176f6;
    font-size: 12px;
    color: #fff;
    line-height: 20px;
    text-align: center;
  }

  .inline-middle {
    display: inline-block;
    vertical-align: middle;
  }

  .user-title {
    width: 85px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
    color: #333;
    font-size: 14px;
    line-height: 20px;
  }

  .user-item {
    color: #333;
    font-size: 12px;
    line-height: 20px;
  }
  .header-avatar {
    background: #ffffff;
    box-shadow: 0 4px 10px 0 rgba(34, 34, 34, 0.1);
    border-radius: 4px;
    width: 140px;
  }
}
</style>
